/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.storeChat.service.impl;

import me.zhengjie.modules.mnt.websocket.ChatSocket;
import me.zhengjie.modules.storeChat.domain.ChatMessages;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.storeChat.repository.ChatMessagesRepository;
import me.zhengjie.modules.storeChat.service.ChatMessagesService;
import me.zhengjie.modules.storeChat.service.dto.ChatMessagesDto;
import me.zhengjie.modules.storeChat.service.dto.ChatMessagesQueryCriteria;
import me.zhengjie.modules.storeChat.service.mapstruct.ChatMessagesMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.OnMessage;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yyj
* @date 2023-11-06
**/
@Service
@RequiredArgsConstructor
public class ChatMessagesServiceImpl implements ChatMessagesService {

    private final ChatMessagesRepository chatMessagesRepository;
    private final ChatMessagesMapper chatMessagesMapper;

    @Override
    public Map<String,Object> queryAll(ChatMessagesQueryCriteria criteria, Pageable pageable){
        Page<ChatMessages> page = chatMessagesRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(chatMessagesMapper::toDto));
    }

    @Override
    public List<ChatMessagesDto> queryAll(ChatMessagesQueryCriteria criteria){
        return chatMessagesMapper.toDto(chatMessagesRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ChatMessagesDto findById(Integer id) {
        ChatMessages chatMessages = chatMessagesRepository.findById(id).orElseGet(ChatMessages::new);
        ValidationUtil.isNull(chatMessages.getId(),"ChatMessages","id",id);
        return chatMessagesMapper.toDto(chatMessages);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ChatMessagesDto create(ChatMessages resources) {
        return chatMessagesMapper.toDto(chatMessagesRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ChatMessages resources) {
        ChatMessages chatMessages = chatMessagesRepository.findById(resources.getId()).orElseGet(ChatMessages::new);
        ValidationUtil.isNull( chatMessages.getId(),"ChatMessages","id",resources.getId());
        chatMessages.copy(resources);
        chatMessagesRepository.save(chatMessages);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            chatMessagesRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ChatMessagesDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ChatMessagesDto chatMessages : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" sender",  chatMessages.getSender());
            map.put(" receiver",  chatMessages.getReceiver());
            map.put(" content",  chatMessages.getContent());
            map.put(" timestamp",  chatMessages.getTimestamp());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<ChatMessages> getMessage(String sender,String receiver) {
        return chatMessagesRepository.getMessages(sender,receiver);
    }


}